/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


#include "filmpage.h"
#include <appcommon.h>
#include <servicemanager.h>
#include <QTimer>
#include <QGraphicsObject>
#include <QDeclarativeItem>
#include <QFileInfo>
#include <QDir>

FilmPage::FilmPage(QObject *parent) : Page(parent) {
    mCategoryModel = NULL;
    mItemModel = NULL;
    topCategoryId = "";
}


int FilmPage::init() {
    this->mFilmPageView = qobject_cast<FilmPageView *>(this->mPageView);
    if(!mFilmPageView) {
        return -1;
    }

    APPINFO << this->property("dbtable").toString();
    mCategoryModel = new DbModel(QString("select * from  %1").arg(this->property("dbtable").toString()));
    if(mCategoryModel->count() <= 0) {
        return -1;
    }

    ImageData *imagedata = NULL;
    for(int i=0; i< mCategoryModel->count(); i++) {
        imagedata = new ImageData();
        imagedata->setId(mCategoryModel->value("id",i).toString());
        imagedata->setName(mCategoryModel->value("name",i).toString());
        imagedata->setIndexImage(QFileInfo("./cn/" + mCategoryModel->value("normal_image",i).toString()).absoluteFilePath());
        imagedata->setSelectImage(QFileInfo("./cn/" + mCategoryModel->value("selected_image",i).toString()).absoluteFilePath());
        _topCategory.append(imagedata);
    }

    topCategoryId = this->property("film_category_id").toString();

    int nRet = loadFilmData();
    APPINFO << topCategoryId<<nRet;
    return nRet;
}

int FilmPage::loadFilmData(void) {
    if(mItemModel) {
        delete mItemModel;
        mItemModel = NULL;
    }

    if(this->property("dbtable").toString() == QString("film_categories")) {
        mItemModel = new DbModel(QString("select * from films where film_category_id = %1").arg(topCategoryId));
    } else if (this->property("dbtable").toString() == QString("music_categories")) {
        mItemModel = new DbModel(QString("select * from musics_list where music_category_id = %1").arg(topCategoryId));
    }

    if(mItemModel->count() <= 0) {
        return -1;
    }

    if(_subCategory.empty() == false) {
        foreach(ImageData * i, _subCategory) {
            delete i;
            i = NULL;
        }
        _subCategory.clear();
    }


    QString indexName ="";
    QString indexImage = "";
//    QString absPath="";

    for(int i=0; i< mItemModel->count(); i++) {
        indexName = mItemModel->value("name",i).toString();
        indexImage = QFileInfo("./cn/" + mItemModel->value("index_image",i).toString()).absoluteFilePath();
//        absPath= "/home/ubuntu/work/work/test/multimedia-gallery/multimediaGallery/cn/";
//        absPath += indexImage;
//        APPINFO << indexImage;
//        QPixmap p(absPath);
//        p=p.scaled(105,150);
//        absPath = absPath.replace(QString("/home/ubuntu/work/work/test/multimedia-gallery/multimediaGallery/cn/images/music/"),
//                                  QString("/tmp/temp/"));
//        APPINFO << absPath;
//        p.save(absPath,"png");
        _subCategory.append(new ImageData(indexName,indexImage));
    }
    return 0;
}



int FilmPage::uninit() {
    if(mCategoryModel) {
        delete mCategoryModel;
        mCategoryModel = NULL;
    }
    if(_topCategory.empty() == false) {
        foreach(ImageData * i, _topCategory) {
            delete i;
            i = NULL;
        }
        _topCategory.clear();
    }
    this->disconnect(SIGNAL(setDefaultIndex()));
    QDeclarativeItem *item = this->mFilmPageView->getQmlView()->rootObject()->findChild<QDeclarativeItem*>("oneItemInListView");
    item->disconnect(SIGNAL(subIconClick(int)));
    return 0;
}

int FilmPage::beforeShow() {
    this->mFilmPageView->showViewUseQml(this);
    QGraphicsObject *rootObject =  (this->mFilmPageView->getQmlView()->rootObject());
    QObject *rootObject_sub = qobject_cast<QObject *>(rootObject);
    QObject::connect(this, SIGNAL(setDefaultIndex()), rootObject_sub, SLOT(setDefaultIndex()));


//    QDeclarativeItem *item = this->mFilmPageView->getQmlView()->rootObject()->findChild<QDeclarativeItem*>("oneItemInListView");
//    APPINFO << item;
//    QObject::connect(item, SIGNAL(subIconClick(int)), this, SLOT(onSubIconClick(int)));

    return 0;
}

int FilmPage::afterShow() {
    return 0;
}

int FilmPage::onClick(int key) {

    return 0;
}

int FilmPage::onTouch(const QString &type, const QHash<QString, QString> *args) {

}

void FilmPage::goNextPage() {

}

QString FilmPage::backGroundImage() const
{
    QDir dir(this->property("backgroundimage").toString());

    return dir.absolutePath();
}

QDeclarativeListProperty<ImageData> FilmPage::topCategory(){
    return QDeclarativeListProperty<ImageData>(this, &_topCategory, &imageDatasPropAppend,
            &imageDatasPropCount, &imageDatasPropAt, &imageDatasPropClear);
}

QDeclarativeListProperty<ImageData> FilmPage::subCategory(){
    return QDeclarativeListProperty<ImageData>(this, &_subCategory, &imageDatasPropAppend,
            &imageDatasPropCount, &imageDatasPropAt, &imageDatasPropClear);
}

void FilmPage::onIconClick(QString id) {
    APPINFO << id;
    if(id == QString("return")) {
        SERVICE(RouterService)->goBack();
        return;
    }

    if(this->topCategoryId == id) {
       return;
    }

    emit setDefaultIndex();
    this->topCategoryId = id;
    loadFilmData();
    emit subCategoryChanged();

}

void FilmPage::onSubIconClick(int id){
    int nId = id;

    if(this->property("dbtable").toString() == QString("film_categories")) {
        QHash<QString, QString> *args = new QHash<QString, QString>();
        args->insert("bill_image", mItemModel->value("bill_image",nId).toString());
        args->insert("detail_image", mItemModel->value("detail_image",nId).toString());
        args->insert("file_url", mItemModel->value("file_url", nId).toString());
        SERVICE(RouterService)->redirectTo(this->property("next_page").toString(), args);
    } else if (this->property("dbtable").toString() == QString("music_categories")) {
        QHash<QString, QString> *args = new QHash<QString, QString>();
        args->insert("bill_image", mItemModel->value("bill_image",nId).toString());
        args->insert("detail_image", mItemModel->value("detail_image",nId).toString());
        args->insert("music_list_id",mItemModel->value("id",nId).toString());
        SERVICE(RouterService)->redirectTo(this->property("next_page").toString(), args);
    }
}










